%%
%% %CopyrightBegin%
%%
%% Copyright Hillside Technology Ltd. 2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%

%% This file was generated by soap:wsdl2erlang, but it has been modified: the 
%% body of the generated skeleton functions has been modified to do something.

%%
%% generated by soap from: e:/e_soap/soap/test/soap_SUITE_data/SendService.wsdl
%% for service "SendService" and port "SendServiceSoap"
%% using options: [{service,"SendService"},{port,"SendServiceSoap"},{generate,both},{namespaces,[{"com.esendex.ems.soapinterface",undefined}]},{http_server,soap_server_cowboy_1},{server_name,"sendService_test_server"},{http_client,soap_client_ibrowse},{client_name,"sendService_test_client"}]

-module(sendService_test_server).

-include("SendService.hrl").

-export(['SendMessage'/3]).
-export(['SendMessageMultipleRecipients'/3]).
-export([interface/0]).

-spec 'SendMessage'(Parsed_body::sendMessage(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_response(sendMessageResponse()).
'SendMessage'(#'sendMessage'{recipient = MSISDN,
                  type = Type,
                  body = Body}, Req, Options) ->
  case Type of 
    "Text" ->
      Result = #'sendMessageResponse'{sendMessageResult = send_sms(MSISDN, Body, Options)},
      io:format("Result: ~p~n", [Result]),
      {ok, Result, Req, Options};
    _ -> 
      Fault_string = io_lib:format("incorrect type, got: ~p, expected \"Text\"", [Type]),
      {fault, soap_fault:fault(client, Fault_string, Req), Req, Options}
  end.

-spec 'SendMessageMultipleRecipients'(Parsed_body::sendMessageMultipleRecipients(),
    Soap_req::soap:soap_req(), State::soap:soap_handler_state())
    -> soap:soap_response(sendMessageMultipleRecipientsResponse()).
'SendMessageMultipleRecipients'(#'sendMessageMultipleRecipients'{recipients = Recipients,
                                    body = Body}, Req, Options) ->
  #'recipients'{recipient = MSISDNS} = Recipients,
  Results = [send_sms(MSISDN, Body, Options) || MSISDN <- MSISDNS],
  Result = #'sendMessageMultipleRecipientsResponse'{
              results = #'results'{result = Results}},
  {ok, Result, Req, Options}.

send_sms(MSISDN, Body, Options) ->
  io:format("send_sms!~n"),
  case validate_msisdn(MSISDN, Options) of
    valid ->
      io:format("sending ~p to ~p~n", [Body, MSISDN]),
      "OK";
    invalid ->
      io:format("MSISDN ~p is not valid~n", [MSISDN]),
      "Not OK"
  end.

%% Options is a list of {Re_pattern, Result}. The first pattern that matches
%% determines the result. If no pattern matches the result is considered valid.
validate_msisdn(_MSISDN, []) ->
  valid;
validate_msisdn(MSISDN, [{Pattern, Result} | T]) -> 
  case re:run(MSISDN, Pattern) of
    {match, _} -> 
      Result;
    _ -> 
      validate_msisdn(MSISDN, T)
  end.

%% The 'interface()' function is used by the SOAP framework to access information about
%% the WSDL.
interface() ->
    ?INTERFACE.
